#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
struct node
{
	int l, r, maxv;
}tr[N*4];
int n, m;
int w[N];
void build(int u, int l, int r)
{
	if (l >= r) {
		tr[u] = { l,r,w[r] };
	}
	else {
		tr[u] = { l,r };
		int mid = l + r >> 1;
		build(u << 1, l, mid);
		build(u << 1 | 1, mid + 1, r);
		tr[u].maxv = max(tr[u << 1].maxv, tr[u << 1 | 1].maxv);
	}
}

int query(int u, int l, int r)
{
	if (tr[u].l >= l && tr[u].r <= r) {
		return tr[u].maxv;
	}
	int mid = tr[u].l + tr[u].r >> 1;
	int max_1 = -1e8;
	if (l <= mid) {
		max_1 = query(u << 1, l, r);
	}
	if (r > mid) {
		max_1 = max(max_1, query(u << 1 | 1, l, r));
	}
	return max_1;
}
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &w[i]);
	}
	build(1, 1, n);
	while (m--) {
		int l, r;
		scanf("%d%d", &l, &r);
		cout << query(1, l, r) << endl;
	}
	return 0;
}